package homework03;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;

public class StoreAttrToTxt {
	@SuppressWarnings("resource")
	public static void main(String[] args) throws IOException, InstantiationException, IllegalAccessException {
		Person person = new Person();
		person.setIdNo("5122245566");
		person.setName("张小平");
		person.setAge(23);
		person.setSex("Male");
		person.setIsMerried(true);

		File file = new File(
				"C:\\Users\\小苏\\git\\Java_KaiFaJiChu_assignment\\DevelopmentBasic\\src\\homework03\\person.txt");
		if (!file.exists()) {
			file.createNewFile();
		}
		byte str[] = new byte[1024];
		String pers = "";
		FileOutputStream in = new FileOutputStream(file);

		Class clazz = Person.class;
		Person person1 = (Person) clazz.newInstance();
		Field[] fields = clazz.getDeclaredFields();

		for (Field f1 : fields) {
			f1.setAccessible(true);
			String field = f1.toString().substring(f1.toString().lastIndexOf(".") + 1);
			pers = (field + "=" + f1.get(person) + "\n");
			str = pers.getBytes();
			in.write(str, 0, str.length);
		}

	}

}
